<?php defined('_JEXEC') or die('Restricted access'); 
/**
* VIDEO CONFERENCES
* Copyright (c) 2011 
* Author: Dmitriy Tovkach (ragvalod@gmail.com)
* @license		GNU/GPL
**/


?>
<script type="text/javascript">
            var $ = jQuery;
            $(document).ready(function(){
                  var iviewer = {};
                  $("#viewer2").iviewer(
                  {
                      src: "<?php echo JURI::base(); ?><?php echo $this->image; ?>",
                      initCallback: function()
                      {
                        iviewer = this;
                      }
                  });
            });
        </script>
<?php //echo "<div style='color:red'>".$this->tokbox['sessionID']."</div>"; ?>

<script type="text/javascript">
    TB.setLogLevel(TB.DEBUG);    
    TB.addEventListener('exception', exceptionHandler);
    var session = TB.initSession('<?php echo $this->tokbox['sessionID']; ?>');     
    session.addEventListener('sessionConnected', sessionConnectedHandler);
    session.addEventListener('streamCreated', streamCreatedHandler);
    session.addEventListener("streamDestroyed", streamDestroyedHandler);
    //session.addEventListener("sessionDisconnected", sessionDisconnectedHandler);
    //session.connect('3903411', '<?php echo $this->tokbox['token']; ?>');
    //session.addEventListener("sessionDisconnected", sessionDisconnectedHandler);
    session.connect('3903411', '<?php if ($this->type_user == "moderator") { echo "moderator_token"; } else echo $this->tokbox['token']; ?>');
    //session.connect('3903411', '<?php echo $this->tokbox['token']; ?>');
    //TB.addEventListener('DestroyConnection',connectionDestroyed);
    var publisher;
    var active_streams = [];
    var my_id = "";
    
    
    function sessionConnectedHandler(event) {
      // Publish my webcam stream and put it in a div
      var divPropsBig = {width: 558, height:496};
      var divPropsSmall = {width: 172, height:172, name:"<?php echo $this->user_data; ?>"};
      //publisher = session.publish('bigview',divPropsBig);
      publisher = session.publish('smallview',divPropsSmall);
      
      
      <?php //if ($this->tokbox['subscribe'] == true) { ?>
      subscribeToStreams(event.streams);
      <?php //} ?>
    }
    function exceptionHandler(event) {
	     alert("Exception:" + event.title + ". " + event.message);
    }
    function streamDestroyedHandler(event) {
	   
       for (var i = 0; i < event.streams.length; i++) {
        var stream = event.streams[i];
        //alert("Stream " + stream.name + " ended. " + event.reason); 
        unsubscribe(stream);
	if (stream.name == "<?php echo $this->user_data; ?>")
	{
	    //alert(stream.name);
	    location.href = "<?php echo JRoute::_('index.php?option=com_community&view=videoconferences', false); ?>";
	}
      }
    }
    function unsubscribe(stream) {
	var subscribers = session.getSubscribersForStream(stream);
	for (var i = 0; i < subscribers.length; i++) {
		session.unsubscribe(subscribers[i]);
		//alert(stream.streamId);
		$("#win"+stream.streamId).remove();
		RebuildWindowsStreams();
	}
    }
    function unsubscribeStream(streamId) {
      
      for (var i = 0; i < active_streams.length; i++) {
        var stream = active_streams[i];
        if (stream.streamId == streamId) {
            unsubscribe(stream);
	    var delete_user = session.getPublisherForStream(stream);
	    session.forceUnpublish(stream);
        }
      }
    }



     function streamCreatedHandler(event) {
      subscribeToStreams(event.streams);
    }
    function subscribeToStreams(streams) {
      for (var i = 0; i < streams.length; i++) {
        // Make sure we don't subscribe to ourself
        if (streams[i].connection.connectionId == session.connection.connectionId) {
          return;
        }
	var cht = "";
	
	if ($("#subscribemembers div.sub-window").length > 0)
	{
	  if (!$("#subscribemembers div.sub-window").last().hasClass('last'))
	  {
		cht = " last";	
          }
        }
	active_streams[i] = streams[i];
        // Create the div to put the subscriber element in to
        $("#subscribemembers").append('<div id="win'+streams[i].streamId+'" class="sub-window'+cht+'"><div style="position:absolute;" id="stream'+streams[i].streamId+'"></div><p class = "caption"><a id="link'+streams[i].streamId+'" rel="'+i+'" href="javascript:void(0);" onclick="SmallToBig('+i+');">'+streams[i].name+'</p><?php if ($this->type_user == "moderator") { echo '<div style="position:absoulte;margin-top:25px;margin-left:30px;"><a href="javascript:void(0);" onclick="unsubscribeStream(\'+streams[i].streamId+\')">Remove Stream</a></div>'; } ?></div>');
	
                           
        // Subscribe to the stream
        session.subscribe(streams[i], 'stream'+streams[i].streamId,{width:172,height:147,style:{nameDisplayMode:"off"}});
	if (streams[i].name == "<?php echo $this->creator_name; ?>")
	{
	    //alert('--');
	    var num = $("#link"+streams[i].streamId).attr("rel");
        SmallToBig(num);
	} else
	{
		SmallToBig(0);
	}
      }
    }
    
    function SmallToBig(num)
    {
	    $("#bigview").html('<div id="streambig"></div>');
	    session.subscribe(active_streams[num], 'streambig',{width: 556, height:496,style:{nameDisplayMode:"off"}});
	    $("#usernamebig").html(active_streams[num].name);
    }
    function RebuildWindowsStreams()
    {
	    $("#subscribemembers div.sub-window").each(function(i){
	         $(this).removeClass("last");
		 if (i % 2)
		 {
	            $(this).addClass("last");
		 }
	    });  
    }
    function FullScreenStream()
    {
	    //$(document.createElement('param')).attr("name", "allowFullScreen").attr("value", "true").appendTo("#bigview object");  
    }
    function UpdateStatusConference()
    {

	$.ajax({
	    url: "<?php echo JRoute::_('index.php?option=com_videoconference&update=1&id='.$this->id_conference.'&format=ajax', false); ?>",
	    success: function(data){
//				alert(data);
	    }
        });    
    }
    setInterval("UpdateStatusConference()",600000);
  </script>
<div class="conftitle"><?php echo $this->title; ?></div>
<div class="confdesc"><?php echo $this->description; ?></div>

 <div class="content" id="media-holder">
    	<div class="fivecolTwo" style="width:364px;height:100px;" id="subscribemembers">
        	<!-- div class = "sub-window"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window last"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window last"><p class = "caption">Name Here, Location Here.</p></div -->
            
            
            <!-- div class = "sub-window last"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window last"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window"><p class = "caption">Name Here, Location Here.</p></div>
            <div class = "sub-window last"><p class = "caption">Name Here, Location Here.</p></div -->        
        
        </div> <!-- fivecolTwo -->
        
        <div class = "fivecolThree">

            <div class = "main-window">
		<div id="bigview" style="position:absolute;"></div>	
            	<div class = "reflection-window" style="position:absolute;top:10px;"><div id="smallview" ></div></div>
            	<p class = "main-movie-caption"><img src="<?php echo JURI::base(); ?>components/com_videoconference/images/video-icon.png" alt ="video icon"><span id="usernamebig"></span><a href ="#"><img class="right" src="<?php echo JURI::base(); ?>components/com_videoconference/images/close.png" alt ="close window button"></a><a href = "#"><img class="right" src="<?php echo JURI::base(); ?>components/com_videoconference/images/enlarge.png" alt ="pop out window button" ></a><a href = ""><img class="right" src="<?php echo JURI::base(); ?>components/com_videoconference/images/min.png" alt ="minimize window button"></a></p>
                <div class = "main-movie-controls left"><a href = "#"><img class="right" src="<?php echo JURI::base(); ?>components/com_videoconference/images/target-windows.jpg" alt ="focus all windows button"></a><a href = "javascript:void();" onclick="FullScreenStream();"><img class="right" src="<?php echo JURI::base(); ?>components/com_videoconference/images/video-icon2.jpg" alt="start chat icon"></a></div>
                
	    </div>
            <div class="notes-window">
            <div id="viewer2" class="viewer"></div>
        </div>
        </div> <!-- fivecolTwo -->
        <div class = "fivecolFive">
        	<div class = "container">
        		<h2 class = "white">Who's Online:</h2>
                <p class ="white">
		<?php
		$count = 0;
		foreach ($this->list_friends as $friend)
		{
	             if ($count !=0)
		     {
		        echo ", ";
		     }
		     $count++;
		     echo '<a href="'.JRoute::_('index.php?option=com_community&view=profile&userid='.$friend['id'], false).'">'.$friend['name'].'</a>';
		     if ($this->type_user == "moderator")
		     {
				echo '<a href="" id="us'.$friend['id'].'"></a>';
				echo '&nbsp;(<a href="javascript:void(0);" onclick="joms.videoconference.addFriendToConf(\''.$friend['id'].'\', \''.$this->id_conference.'\');">'.JText::_('COM_COMMUNITY_ADD').'</a>)';
		     }	
		}
		?>
	        </p>
	    </div><!-- whos-online -->
        </div> <!-- fivecolFive -->
        <p class="clear"></p>
	<?php if ($this->type_user == "moderator") { ?>
		<a href="<?php echo JRoute::_('index.php?option=com_videoconference&task=endconference&id='.$this->id_conference, false); ?>" class="endconf">End Videconference</a>  
	<?php } else { ?>
		<a href="<?php echo JRoute::_('index.php?option=com_community&view=videoconferences', false); ?>" class="leaveconf">Leave Video Conference</a>
	<?php } ?>
    </div>
